package com.itheima.exerciseTest.test60_69;

import java.io.*;
import java.util.ArrayList;

/*
    . 分析以下需求，并用代码实现
	项目根路径下有个questions.txt文件内容如下：
	5+5
	150-25
	155*155
	2555/5
	要求：读取内容计算出结果，将结果写入到results.txt文件中
	5+5=10
	//....
	思考:如果说读取questions.txt文件的内容,算出计算结果,再写入questions.txt文件,即读和写的操作时针对同一个文件,应该如何操作
 */
public class Test67 {
    public static void main(String[] args) throws IOException {
        // 创建字符缓冲输入流
        BufferedReader br;
        br = new BufferedReader(new FileReader("backBurner\\questions.txt"));

        ArrayList<String> al = new ArrayList<>();
        String str = null;
        while ((str = br.readLine()) != null) {
            al.add(str + "");
        }

        for (int i = 0; i < al.size(); i++) {
            String s = al.get(i);
            if (s.contains("+")) {
                String[] split = s.split("\\+");
                int num1 = Integer.parseInt(split[0]);
                int num2 = Integer.parseInt(split[1]);
                int num = num1 + num2;
                al.set(i, s + "=" + num);
            } else if (s.contains("-")) {
                String[] split = s.split("-");
                int num1 = Integer.parseInt(split[0]);
                int num2 = Integer.parseInt(split[1]);
                int num = num1 - num2;
                al.set(i, s + "=" + num);
            } else if (s.contains("*")) {
                String[] split = s.split("\\*");
                int num1 = Integer.parseInt(split[0]);
                int num2 = Integer.parseInt(split[1]);
                int num = num1 * num2;
                al.set(i, s + "=" + num);
            } else if (s.contains("/")) {
                String[] split = s.split("/");
                int num1 = Integer.parseInt(split[0]);
                int num2 = Integer.parseInt(split[1]);
                int num = num1 / num2;
                al.set(i, s + "=" + num);
            } else {
                System.out.println(s + "不在考虑范围内");
            }
        }

        // 创建字符缓冲输出流
        BufferedWriter bw;
        bw = new BufferedWriter(new FileWriter("backBurner\\results.txt"));
        for (String s : al) {
            bw.write(s);
            bw.newLine();
        }
        br.close();
        bw.close();

    }
}
